package cn.lblbc.lib.view.selectRv

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.lblbc.lib.R

class LblSelectRecyclerView<T>(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    private var adapter: LblSelectRvAdapter<T>
    private var rv: RecyclerView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.lbl_view_rv, this, true)
        adapter = LblSelectRvAdapter()
        rv = view.findViewById(R.id.recyclerView)
        rv.adapter = adapter
        rv.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
    }

    fun setLayoutResId(getLayoutResId: () -> Int) {
        adapter.getLayoutResId = getLayoutResId
    }

    fun setCheckBoxResId(getCheckBoxResId: () -> Int) {
        adapter.getCheckBoxResId = getCheckBoxResId
    }

    fun setonSelectionChanged(onSelectionChanged: (selectionItemList: List<T>) -> Unit) {
        adapter.onSelectionChanged = onSelectionChanged
    }

    fun setOnBind(onBind: (itemView: View, data: T) -> Unit) {
        adapter.onBind = onBind
    }

    fun setData(goodsList: List<T>) {
        adapter.setData(goodsList)
    }

    fun clearData() {
        adapter.clearData()
    }

    fun getSelectedItems() = adapter.selectionItemList
}